package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    @Override    //分页查询套餐
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        Page<Setmeal> page = new Page<>(pageNum, pageSize);

        //模糊查询
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //分页查询
        page = setmealMapper.selectPage(page, setmealLambdaQueryWrapper);
        //通过category_id查询category
        List<Setmeal> setmealList = page.getRecords();//TODO 这里setmealList 和 page.getRecords() 是同一块地址吗?
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());//返回套餐分类

                //查询套餐菜品关系表
                LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDishLambdaQueryWrapper);
                setmeal.setSetmealDishes(setmealDishes);
            }
        }
        return page;
    }

    @Override   //新增保存套餐
    public void save(Setmeal setmeal) {
        //基本信息保存至setmeal表中
        setmealMapper.insert(setmeal);

        //套餐内菜品信息setmeal_dishes表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //设置id
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }


    }

    @Override   //启售/停售 (批量)
    public void updateStatus(Integer status, Long[] ids) {
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                Setmeal setmeal = new Setmeal();
                setmeal.setId(id);
                setmeal.setStatus(status);
                setmealMapper.updateById(setmeal);
            }
        }
    }

    @Override   //删除套餐
    public void deleteByIds(Long[] ids) {
        //判断要删除的套餐中是否有在售的
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);//是否在售  1表示在售
        Integer count = setmealMapper.selectCount(setmealLambdaQueryWrapper);
        if (count > 0) {//表示存在在售套餐
            throw new CustomException("选中套装中含有在售,禁止删除");
        }

        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
                //删除中间表中此套餐的信息
                setmealDishMapper.delete(setmealDishLambdaQueryWrapper);

                //根据id删除套餐表中的套餐信息
                setmealMapper.deleteById(id);
            }
        }
    }

    @Override   //修改套餐
    public void update(Setmeal setmeal) {
        //修改套餐基本信息
        setmealMapper.updateById(setmeal);

        //修改套餐关联的菜品集合
        //先删后改?
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(setmealDishLambdaQueryWrapper);//删除旧的信息

        //添加新信息
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override   //回显套餐
    public Setmeal findById(Long id) {
        //1. 查询套餐基本信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //2. 根据category_id从category表中查询name
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        //3. 查询套餐关联的菜品集合
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(setmealDishLambdaQueryWrapper);
        //将查询到的信息放入setmeal
        setmeal.setSetmealDishes(setmealDishList);

        return setmeal;
    }

    @Override
    public List<Setmeal> findDishOnSale(Long categoryId, Integer status) {
        //1. 查询套餐基本信息
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);
        List<Setmeal> setmealList = setmealMapper.selectList(setmealLambdaQueryWrapper);

//        if (CollectionUtil.isNotEmpty(setmealList)) {
//            for (Setmeal setmeal : setmealList) {
//                //2. 根据category_id从category表中查询name
//                Category category = categoryMapper.selectById(setmeal.getCategoryId());
//                setmeal.setCategoryName(category.getName());
//
//                //3. 查询套餐关联的菜品集合
//                LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
//                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(setmealDishLambdaQueryWrapper);
//                //将查询到的信息放入setmeal
//                setmeal.setSetmealDishes(setmealDishList);
//            }
//        }
        return setmealList;
    }

    @Override   //查看在售套餐具体菜品信息
    public List<SetmealDish> findByIdBrief(Long id) {
        //1. 查询套餐基本信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //2. 查询套餐关联的菜品集合
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(setmealDishLambdaQueryWrapper);

        //3.查询菜品表,拿到菜品的图片信息
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                //将菜品的图片信息放入setmealDish
                setmealDish.setImage(dish.getImage());
                setmealDish.setDescription(dish.getDescription());
            }
        }
        return setmealDishList;
    }
}
